package com.dragons.dragonsvf.controller.bak;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragons.dragonsvf.controller.LoginCtrl;
import com.dragons.dragonsvf.dao.MAuthMapper;
import com.dragons.dragonsvf.entities.*;
import com.dragons.dragonsvf.utils.netUtils;
import com.dragons.dragonsvf.utils.secret.AesEncryptUtil;
import com.dragons.dragonsvf.utils.secret.FixedSM4Util;
import com.dragons.dragonsvf.utils.secret.SecureEncryptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Dragons.G
 * @date 2025-06-26 11:55:04
 * @description 后台控制器
 */
@RestController
public class MAuthCtrl {

    private static final Logger logger = LoggerFactory.getLogger(LoginCtrl.class);

    @Autowired
    private MAuthMapper mAuthMapper;

    @RequestMapping("/mLogin")
    public ResponseDTO mLogin(@RequestBody MAuthDTO user) {

        ResponseDTO res = new ResponseDTO();

        String encrypt = "";
        if(!"".equals(user.getEnAcross())) {
            try {
                encrypt = FixedSM4Util.encryptNoneKeyIvCBC(user.getEnAcross());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 根据名字查询是否存在这个用户
        QueryWrapper<MAuth> u = new QueryWrapper<>();
        u.eq("m_user", user.getUsr())
            .select("*");
        List<MAuth> isHasUser = mAuthMapper.selectList(u);

        QueryWrapper<MAuth> u1 = new QueryWrapper<>();
        u1.eq("m_user", user.getUsr())
            .eq("mh_pwd_en", encrypt) // 根据用户输入加密后的密码
            .select("*");
        List<MAuth> userInfos = mAuthMapper.selectList(u1);

        if (isHasUser.size() > 0) { // 验证用户是否存在
            if(userInfos.size() > 0) { // 验证密码
                res.setCode(200);
                res.setMsg("登录成功");
                MAuth mAuth = userInfos.get(0);
                String avatarBase64 = netUtils.convertToBase64(mAuth.getM_avatar_url());

                MAuthDTO mAuthDTO = new MAuthDTO();
                // 复制属性（注意参数顺序：源对象在前，目标对象在后）
                BeanUtils.copyProperties(mAuth, mAuthDTO);
                mAuthDTO.setId(mAuth.getId());
                mAuthDTO.setUsr(mAuth.getM_user());
                mAuthDTO.setEnAcross(mAuth.getMh_pwd_en());
                mAuthDTO.setEnType(mAuth.getM_encryption_algorithm());
                mAuthDTO.setEmail(mAuth.getM_email());
                mAuthDTO.setTel(mAuth.getM_tel());
                mAuthDTO.setWechat(mAuth.getM_wechat());
                mAuthDTO.setAvatar(mAuth.getM_avatar_url());
                mAuthDTO.setRole(mAuth.getM_role_id());
                mAuthDTO.setIsFrozen(mAuth.getM_is_frozen());
                mAuthDTO.setCreateTm(mAuth.getM_created_at());
                mAuthDTO.setUpdateTm(mAuth.getM_updated_at());

                mAuthDTO.setAvatarBase64(avatarBase64);

                res.setData(mAuthDTO);
            } else {
                res.setCode(200);
                res.setMsg("密码错误");
            }
        } else {
            res.setCode(200);
            res.setMsg("用户不存在");
        }
        return res;

    }


    @RequestMapping("/resetManagePwd")
    public ResponseDTO resetManagePwd(@RequestBody MAuthDTO user) {


        ResponseDTO res = new ResponseDTO();
        res.setCode(200);
        res.setMsg("用户不存在");
        return res;
    }




}
